package com.fingard.dsp.bank.directbank.lu11;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.FtpSet;
import com.fingard.dsp.bank.directConfig.TaskTimerSet;
import com.fingard.dsp.bank.directbank.bankInterface.IScheduleTask;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.net.FtpReceiver;
import com.fingard.net.SftpPortClient;
import com.fingard.text.StringHelper;
import com.fingard.util.ZipHelper;
	
/**
 * 陆金所定时任务
 * 
 * 需求：定时轮询富聪SFTP服务器是否存在上传文件，不存在则本地创建空文件上传
 * @author fanghh
 * @date 2018/7/26
 */
public class LU11ScheduleTask extends LU11Base implements IScheduleTask {
	@Override
	public void execTask(TaskTimerSet timerSet) {
		 try {
	            if (timerSet.taskNameList.toLowerCase().contains("automakefile")) {
	                autoMakeFile();
	            }
	        } catch (Exception ex) {
	            this.WriteBankLogLn(ex);
	        }
	}
	
	public void autoMakeFile(){
		SftpPortClient sftpPortClient = null;
    	try {
    		Date tmpDateNow = new Date();
    		
    		String nowDate = new SimpleDateFormat("yyyyMMdd").format(tmpDateNow);
			if(bankFront.customMap.containsKey("lastUpFileDate")){
				if(nowDate.equals(bankFront.customMap.get("lastUpFileDate"))){
					System.out.println("LU11当天不需再产生空文件");
					return;
				}
			}
    		
			String tmpDateHour = Format.Time8Format.format(tmpDateNow).substring(0, 5);
			String tmpMinutes = getBankConfig().getValueAsString("taskTimeHourMinutes");
			if (StringHelper.hasAnyChar(tmpMinutes) && tmpDateHour.equals(tmpMinutes)) {
				
				FtpSet ftpSet = bankFront.getFtpSet("refund_upload");
				sftpPortClient = new SftpPortClient(ftpSet);
				String  directDir = "";
				String tmpDate = Format.Date8Format.format(tmpDateNow);
				if (ftpSet.upDir.endsWith("/")) {
					directDir = tmpDate;
				} else {
					directDir = "/" + tmpDate;
				}
				
				WriteBankLogLn2("开始遍历服务器目录:" + ftpSet.upDir + directDir);
				FtpReceiver ftpListFileReceiver = sftpPortClient.listFiles(ftpSet.upDir + directDir);
				if (ftpListFileReceiver.isCompleted) {
					WriteBankLogLn2("遍历完成,无需上传空文件!");
					bankFront.customMap.put("lastUpFileDate", nowDate);
					return;
				}
				
				String refundFileDir = getRefundFileDirectory();
	    		String tmpBatSeqNo = String.valueOf(getBatSeqNo(refundFileDir));
				String refundFileName = bankFront.org + "_refundResult_" + Format.Date8Format.format(tmpDateNow) + "_" + tmpBatSeqNo;
				String localRefundFilePath = refundFileDir + File.separator + refundFileName + ".csv";
				WriteBankLogLn2("本地退保文件路径: " + localRefundFilePath);
				FileHelper.writeFile(localRefundFilePath, "", getCharset());
				
				// 压缩文件
				String localRefundZipFilePath = refundFileDir + File.separator + refundFileName + ".zip";
				WriteBankLogLn2("本地退保文件压缩包路径: " + localRefundZipFilePath);
				FileOutputStream fos = new FileOutputStream(new File(localRefundZipFilePath));
				ZipHelper.toZip(localRefundFilePath, fos, true);

				// 上传退保文件
				String sftpFilePath = StringHelper.trimEnd(ftpSet.upDir, "/") + "/" + tmpDate + "/" + refundFileName + ".zip";
				WriteBankLogLn2("SFTP服务器压缩文件上传路径:" + sftpFilePath);
				FtpReceiver ftpReceiver = sftpPortClient.uploadFile(directDir, localRefundZipFilePath, sftpFilePath);
				if(StringHelper.hasAnyChar(ftpReceiver.connMsg)){
					this.WriteBankLogLn("上传返回消息：" + ftpReceiver.connMsg);
				}
				
				if (ftpReceiver.isCompleted) {
					WriteBankLogLn2("空文件上传成功");
					bankFront.customMap.put("lastUpFileDate", nowDate);
				} else if (ConnState.FAIL.equals(ftpReceiver.connState)) {
					WriteBankLogLn2("空文件上传通讯失败");
				} else {
					WriteBankLogLn2(ftpReceiver.connMsg);
				}
			}else{
				System.out.println("LU11未到执行小时数");
			}
    	}catch(Exception ex){
    		WriteBankLogLn(ex);
    	}finally{
    		if(sftpPortClient != null){
    			try {
    				sftpPortClient.disconnect();
				} catch (IOException e) {
					e.printStackTrace();
					WriteBankLogLn(e);
				}
    		}
    	}
    }
}
